﻿using BCCommon;
using BCCommon.Http;
using BCDto.Common.BankCardDetection;
using BCDto.Common.SystemExceptionRecord;
using BCService.Common.ExceptionService;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace BCService.CommonService.BankCardDetection
{
    public class BankCardDetectionService : IBankCardDetectionService
    {
        private readonly ISystemExceptionRecordService exceptionRecordService;
        private string host = GeneralContainer.Configuration["AliyunBankCardDetection:host"];
        private string path = GeneralContainer.Configuration["AliyunBankCardDetection:path"];
        private string appcode = GeneralContainer.Configuration["AliyunBankCardDetection:appcode"];

        public BankCardDetectionService(ISystemExceptionRecordService exceptionRecordService)
        {
            this.exceptionRecordService = exceptionRecordService;
        }

        public BankCardDetectionDto RealnameAuthentication(string cardNo, string certNo, string name, string phone)
        {
            name = HttpUtility.UrlDecode(name, Encoding.UTF8);
            string url = host + path;
            string bodys = string.Format("cardNo={0}&certNo={1}&name={2}&phone={3}&seqId=&transDate=&transTime=", cardNo, certNo, name, phone);

            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "APPCODE " + appcode);
            HttpRequestDto httpRequestDto = new HttpRequestDto()
            {
                Headers = headers,
                Url = url,
                Method = MethodType.POST,
                ContentType = FormatType.FormUrlencoded,
                Encoding = Encoding.UTF8.ToString(),
                Content = Encoding.UTF8.GetBytes(bodys)
            };
            try
            {
                HttpHandle http = new HttpHandle();
                /*匹配结果：0000匹配其他不匹配*/

                //TODO:这里会出现空引用问题，待修复，需要仔细联调
                HttpResponseDto response = http.HttpSendRequest(httpRequestDto);
                //{"respCode":"0000","result":"9","respMsg":"鉴权成功"}
                BankCardDetectionResult bankCardDetectionResult = JsonConvert.DeserializeObject<BankCardDetectionResult>(response.ResponseMessage);
                if (bankCardDetectionResult.respCode != "0000")
                {
                    throw new ArgumentException(bankCardDetectionResult.respMsg);
                }
                BankCardDetectionDto bankCardDetectionDto = new BankCardDetectionDto();
                bankCardDetectionDto.CardNo = cardNo;
                bankCardDetectionDto.CertNo = certNo;
                bankCardDetectionDto.Name = name;
                bankCardDetectionDto.Phone = phone;
                bankCardDetectionDto.DetectionResult = bankCardDetectionResult;
                return bankCardDetectionDto;
            }
            catch (Exception ex)
            {
                SystemExceptionRecordDto systemExceptionRecordDto = new SystemExceptionRecordDto();
                systemExceptionRecordDto.Data = JsonConvert.SerializeObject(ex.Data);
                systemExceptionRecordDto.Name = "银行卡认证接口";
                systemExceptionRecordDto.Message = ex.Message;
                systemExceptionRecordDto.Source = ex.Source;
                systemExceptionRecordDto.StackTrace = ex.StackTrace;
                systemExceptionRecordDto.HelpLink = ex.HelpLink;
                systemExceptionRecordDto.HResult = ex.HResult;
                systemExceptionRecordDto.InnerException = JsonConvert.SerializeObject(ex.InnerException);

                this.exceptionRecordService.Create(systemExceptionRecordDto);
                throw;
            }
        }

    }
}
